
set more off
tempfile agwage
use "$data\aghion_stateindustrypanel.dta", clear

* create comparable statenm variable
g statenm = ""
replace statenm = "Andhra Pradesh" if statename == "Andra Pradesh : 1"
replace statenm = "Assam" if statename == "Assam : 2"
replace statenm = "Bihar" if statename == "Bihar : 3"
replace statenm = "Gujarat" if statename == "Gujarat : 4"
replace statenm = "Haryana" if statename == "Haryana : 5"
replace statenm = "Jammu & Kashmir" if statename == "Jammu & Kashmir : 7"
replace statenm = "Karnataka" if statename == "Karnataka : 8"
replace statenm = "Kerala" if statename == "Kerala : 9"
replace statenm = "Madhya Pradesh" if statename == "Madhya Pradesh : 10"
replace statenm = "Maharashtra" if statename == "Maharashtra : 11"
replace statenm = "Orissa" if statename == "Orissa : 14"
replace statenm = "Punjab" if statename == "Punjab : 15"
replace statenm = "Rajasthan" if statename == "Rajasthan : 16"
replace statenm = "Tamil Nadu" if statename == "Tamil Nadu : 18"
replace statenm = "Uttar Pradesh" if statename == "Uttar Pradesh : 20"
replace statenm = "West Bengal" if statename == "West Bengal : 21"

sort statenm
merge m:1 statenm using "$data\wheatrice80.dta" /* this is data on wheat and rice cultivation by state*/
drop _merge

* bring in ag wage
preserve
use "$data\yield_rain_longseries.dta", clear
duplicates drop asicode year, force /*one duplicate district in yield data needs to be dropped*/
collapse wage, by(statenm year)
sort statenm year
save `agwage'
restore

sort statenm year
merge m:1 statenm year using `agwage'
drop _merge

* fixing error in labor reg strictness coding
g strictness = .
replace strictness = 0 if nstrict==0
replace strictness = 1 if prow==1
replace strictness = -1 if proe == 1


*controls
foreach var in delicense cmhdlft wage FDIreform {
	ge `var'80=`var'
	replace `var'80=. if year~=1980
	bys statenic3 year: egen new`var'=total(`var'80)
	drop `var'80
	rename new`var' `var'80
	}


*delicense and wheat area variable missing for some districts so i fill them in here*
bys state (year): egen meandel=mean(delicense)
replace delicense=meandel if year>1979 /*only keeping aghion years*/
bys state: egen meanwheatareaper80=mean(wheatareaper80)
replace wheatareaper80=meanwheatareaper80 if wheatareaper80==.
bys state: egen meanriceareaper80=mean(riceareaper80)
replace riceareaper80=meanriceareaper80 if riceareaper80==.
bys state: egen meancultarea=mean(cultarea)
replace cultarea=meancultarea if cultarea==.


* gen nic code dummy for 200 and 300, then interact with important vars
g twonic = nic3 > 199 & nic3 < 300
replace twonic = . if nic3 == .
g threenic = nic3 > 299 & nic3 < 400
replace threenic = . if nic3 == .
foreach x in shockpctile proe_shockpctile prow_shockpctile proe prow {
	g twonic_`x' = twonic*`x'
}

foreach x in shockpctile proe_shockpctile prow_shockpctile proe prow {
	g threenic_`x' = threenic*`x'
}	
egen shockind = group(shockpctile nic3)


